package com.aiju.cash.ui.account.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.account.entity.ItemList;
import com.aiju.cash.ui.account.manager.AccountTypeManager;
import com.aiju.utils.DateUtil;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.Util;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-04-06 17:13
 * @version: V1.0
 * @Description: 账本详情
 */
public class AccountInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private List<ItemList.DateItem> categoryList;
    private List<ItemList.Item> teamList = new ArrayList<>();

    public AccountInfoAdapter(List<ItemList.DateItem> categoryList) {
        this.categoryList = categoryList;
        this.teamList = new ArrayList<>();
        for (ItemList.DateItem dataItem : categoryList) {
            teamList.addAll(dataItem.getList());
        }
    }

    public boolean isEmpty() {
        return Util.isEmpty(teamList);
    }

    public void dataChange(List<ItemList.DateItem> categoryList) {
        teamList.clear();
        this.categoryList = categoryList;
        for (ItemList.DateItem dataItem : categoryList) {
            teamList.addAll(dataItem.getList());
        }
        notifyDataSetChanged();
    }

    //获取当前球队的类型
    public int getSortType(int position) {
        int sort = -1;
        int sum = 0;
        for (int i = 0; i < categoryList.size(); i++) {
            if (position >= sum) {
                sort++;
            } else {
                return sort;
            }
            sum += categoryList.get(i).getList().size();
        }
        return sort;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_info, parent, false);
        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final ItemList.Item item = teamList.get(position);

        viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(item);
                }
            }
        });

        viewHolder.timeTextView.setText(DateUtil.getStringByFormat(item.getModifyDate(), DateUtil.dateFormatHM));
        viewHolder.userNameTextView.setText(item.getOperatorName());
        viewHolder.picImageView.setImageResource(AccountTypeManager.getPic(item.getCategoryName()));
        viewHolder.nameTextView.setText(item.getCategoryName());
        viewHolder.remarkTextView.setText(item.getRemark());
        viewHolder.moneyTextView.setText(item.getMoney());

    }

    //=====
    @Override
    public long getHeaderId(int position) {
        return getSortType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.header_account, viewGroup, false);
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
        ItemList.DateItem dateItem = categoryList.get(getSortType(position));

        Spannable WordtoSpan = new SpannableString(dateItem.getYMdate() + "\n" + dateItem.getDay() + "日");
        WordtoSpan.setSpan(new AbsoluteSizeSpan(21, true), 9, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        WordtoSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), 9, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        WordtoSpan.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 9, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.dayTextView.setText(WordtoSpan);
        holder.inTextView.setText("收 " + PriceUtil.formatDouble(dateItem.getDaysum().getIncome()));
        holder.outTextView.setText("支 " + PriceUtil.formatDouble(dateItem.getDaysum().getCost()));

    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView dayTextView;

        TextView inTextView;
        TextView outTextView;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            dayTextView = (TextView) itemView.findViewById(R.id.tv_budget_day);

            inTextView = (TextView) itemView.findViewById(R.id.tv_budget_in);
            outTextView = (TextView) itemView.findViewById(R.id.tv_budget_out);
        }
    }

    @Override
    public int getItemCount() {
        return teamList.size();
    }


    private class ContentViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        TextView timeTextView;
        TextView userNameTextView;
        ImageView picImageView;

        TextView nameTextView;
        TextView remarkTextView;
        TextView moneyTextView;


        public ContentViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            timeTextView = (TextView) itemView.findViewById(R.id.tv_account_time);
            userNameTextView = (TextView) itemView.findViewById(R.id.tv_account_user_name);
            picImageView = (ImageView) itemView.findViewById(R.id.iv_account_pic);

            nameTextView = (TextView) itemView.findViewById(R.id.tv_account_name);
            remarkTextView = (TextView) itemView.findViewById(R.id.tv_account_remark);
            moneyTextView = (TextView) itemView.findViewById(R.id.tv_account_money);
        }
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(ItemList.Item item);
    }

}
